package java8.stream;

import org.junit.Test;

import java.math.BigInteger;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Create {

    // 使用Stream.generate() Stream.iterate() Stream.of
    @Test
    public void test1() {
        //Stream.generate()
        Stream<String> stream = Stream.generate(() -> "user").limit(20);
        stream.forEach(System.out::println);
        //此处会报错
        //stream.forEach(System.out::println);

        // Stream.iterate()
        Stream<BigInteger> bigIntStream = Stream.iterate(BigInteger.ZERO, n -> n.add(BigInteger.ONE)).limit(10);
        BigInteger[] bigIntArr = bigIntStream.toArray(BigInteger[]::new);
        System.out.println(Arrays.toString(bigIntArr));

        // Stream.of()
        stream = Stream.of("ma", "zhi", "chu", "is", "java", "developer", "family");
        stream.forEach(System.out::print);
    }

    // 使用数组
    @Test
    public void test2() {

        //int 类型数组要用
        //Arrays.stream(a).boxed();

        String[] arr = {"ma", "zhi", "chu", "is", "java", "developer", "family"};
        Stream<String> stream = Stream.of(arr);
        stream.forEach(System.out::print);
        String[] stringArr = {"ma", "zhi", "chu", "love"};
        Stream<String> stream1 = Arrays.stream(stringArr);
        stream1.forEach(System.out::print);
    }

    // 使用集合创建
    @Test
    public void test3() {
        List<String> list = new ArrayList<>();
        list.add("ma");
        list.add("zhi");
        list.add("chu");
        Stream<String> stream2 = list.stream();
        stream2.forEach(System.out::print);
    }

    //使用流行的APIs，如Pattern.compile().splitAsStream()
    @Test
    public void test5() {
        // 5、使用Popular APIs
        String sentence = "ma zhi chu is a Java wechat official account.";
        Stream<String> wordStream = Pattern.compile("\\W").splitAsStream(sentence);
        String[] wordArr = wordStream.toArray(String[]::new);
        System.out.println(Arrays.toString(wordArr));
    }

    @Test
    public void test6() {
        List<String> list = new ArrayList<>();
        list.add("ma");
        list.add("zhi");
        list.add("chu");
        Stream<String> stream2 = list.stream();

        Map<String,String> map = new HashMap();
        List<String> collect = map.keySet().stream().sorted(Comparator.comparing(map::get).reversed()).limit(1).collect(Collectors.toList());


        //Collectors.toMap()
    }


}
